package cn.stylefeng.guns.modular.gridsystem.timer;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.core.timer.TimerTaskRunner;
import cn.stylefeng.guns.modular.gridsystem.entity.*;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizDeviceStatusAssessParam;
import cn.stylefeng.guns.modular.gridsystem.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 状态评估定时任务管理
 */
@Component
@Slf4j
public class DeviceStatueAssessTaskRunner implements TimerTaskRunner {

    @Resource
    private BizDeviceStatusAssessService bizDeviceStatusAssessService;

    @Resource
    private BizDeviceStatusAssessInfoService bizDeviceStatusAssessInfoService;

    @Resource
    private BizExceptionLogService bizExceptionLogService;

    @Resource
    private BizExceptionService bizExceptionService;

    @Resource
    private BizUploadScoreService bizUploadScoreService;

    @Override
    public void action() {
        //查询日志表总的上传数量：正常+故障
        QueryWrapper<BizUploadScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(1) as allAcount","device_id as deviceId");
        queryWrapper.groupBy("device_id");
        List<Map<String, Object>> list = bizUploadScoreService.listMaps(queryWrapper);
        final BizDeviceStatusAssessParam bizDeviceStatusAssessParam = new BizDeviceStatusAssessParam();
        for (Map<String, Object> map : list){
            Integer deviceId = Integer.parseInt(map.get("deviceId").toString());
            //查询设备对应故障
            final QueryWrapper<BizException> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("device_id",deviceId);
            final List<BizException> exceptionList = bizExceptionService.list(queryWrapper1);
            int count = 0;
            final List<Integer> exceptionIds = new ArrayList<>();
            for (BizException bizException:exceptionList){
                final Integer exceptionId = bizException.getId();
                //记录子主表故障关联id
                exceptionIds.add(exceptionId);
                //查询故障数量
                final QueryWrapper<BizExceptionLog> wrapper = new QueryWrapper<>();
                wrapper.eq("exception_id",exceptionId);
                count += bizExceptionLogService.count(wrapper);
            }
            //总数量
            final int allAcount = Integer.parseInt(map.get("allAcount").toString());
            bizDeviceStatusAssessParam.setNormalNumber(allAcount - count);
            bizDeviceStatusAssessParam.setErrorNumber(count);
            bizDeviceStatusAssessParam.setDeviceId(deviceId);
            bizDeviceStatusAssessParam.setAssessTime(DateUtil.date());
            //添加故障评估主表
            final Integer assessId = bizDeviceStatusAssessService.add(bizDeviceStatusAssessParam);
            log.info("添加结果assessId为：{}",assessId);
            //添加故障评估字表
            final BizDeviceStatusAssessInfo bizDeviceStatusAssessInfo = new BizDeviceStatusAssessInfo();
            bizDeviceStatusAssessInfo.setAssessId(assessId);
            bizDeviceStatusAssessInfo.setExceptionIds(StringUtils.join(exceptionIds.toArray(),","));
            bizDeviceStatusAssessInfoService.save(bizDeviceStatusAssessInfo);
        }
    }

}
